<template>
  <el-dialog v-if="visible" :visible="visible" width="500px" title="创建剧场" :append-to-body="true" :close-on-click-modal="false" @close="cancel">
    <el-form ref="form" v-loading="loading" :model="form" :rules="formRules" label-width="120px" label-position="left">
      <el-form-item label="剧场名称" prop="name">
        <el-input v-model="form.name" :clearable="true" placeholder="请输入剧场名称" @clear="$refs.form.validateField(['name'])" />
      </el-form-item>
      <el-form-item label="剧场图片" prop="pictures">
        <upload :file-list="pictures" @on-update="picturesUpdate" />
      </el-form-item>
      <el-form-item label="简介" prop="introduce">
        <el-input ref="introduce" v-model="form.introduce" type="textarea" :rows="8" resize="none" :clearable="true" placeholder="请输入演员简介" />
        <div style="text-align: right">
          <el-button type="danger" size="mini" @click="form.introduce = ''; $refs.introduce.focus(); $refs.form.validateField(['introduce'])">清空</el-button>
        </div>
      </el-form-item>
      <el-form-item label="演出地址" prop="show_address">
        <el-input v-model="form.show_address" :clearable="true" placeholder="请输入演出地址" @clear="$refs.form.validateField(['show_address'])" />
      </el-form-item>
      <el-form-item label="演出日期" prop="show_dates">
        <el-date-picker
          v-model="form.show_dates"
          type="date"
          format="yyyy-MM-dd"
          value-format="yyyy-MM-dd"
          placeholder="请选择演出日期"
          style="width: 100%"
          :clearable="true"
          @change="$refs.form.validateField(['show_dates'])"
        />
      </el-form-item>
      <el-form-item label="演出时间" prop="show_times">
        <el-time-picker
          v-model="form.show_times"
          format="HH:mm"
          value-format="HH:mm"
          default-value="0"
          placeholder="请选择演出时间"
          style="width: 100%"
          :clearable="true"
          @change="$refs.form.validateField(['show_times'])"
        />
      </el-form-item>
      <el-form-item label="状态" prop="status">
        <el-radio-group v-model="form.status">
          <el-radio :label="0">显示</el-radio>
          <el-radio :label="1">隐藏</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="排序" prop="sort">
        <el-input v-model="form.sort" :clearable="true" placeholder="请输入排序" @clear="$refs.form.validateField(['sort'])" />
      </el-form-item>
    </el-form>
    <template #footer>
      <el-button @click="cancel">取消</el-button>
      <el-button v-loading="loading" type="primary" @click="enter">确认</el-button>
    </template>
  </el-dialog>
</template>
<script>
import { addShow } from '@/api/show'
export default {
  name: 'Create',
  props: {
    visible: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      form: {
        name: '',
        pictures: '',
        introduce: '',
        show_address: '',
        show_dates: '',
        show_times: '',
        status: 0,
        sort: ''
      },
      pictures: [],
      formRules: {
        name: [
          {
            trigger: 'blur', validator(rule, value, callback) {
              if (typeof value !== 'string') {
                return callback(new Error('剧场名称不能为空'))
              }
              if (value.trim().length === 0) {
                return callback(new Error('剧场名称不能为空'))
              }
              return callback()
            }
          }
        ],
        pictures: [
          {
            trigger: 'blur',
            validator(rule, value, callback) {
              if ((typeof value) !== 'string') {
                return callback(new Error('剧场图片不能为空'))
              }
              if (value.trim().length === 0) {
                return callback(new Error('剧场图片不能为空'))
              }
              return callback()
            }
          }
        ],
        introduce: [
          {
            trigger: 'blur',
            validator(rule, value, callback) {
              if (typeof value !== 'string') {
                return callback(new Error('简介不能为空'))
              }
              if (value.trim().length === 0) {
                return callback(new Error('简介不能为空'))
              }
              return callback()
            }
          }
        ],
        show_address: [
          {
            trigger: 'blur', validator(rule, value, callback) {
              if (typeof value !== 'string') {
                return callback(new Error('演出地址不能为空'))
              }
              if (value.trim().length === 0) {
                return callback(new Error('演出地址不能为空'))
              }
              return callback()
            }
          }
        ],
        show_dates: [
          {
            trigger: 'blur',
            validator(rule, value, callback) {
              if (typeof value !== 'string') {
                return callback(new Error('演出日期不能为空'))
              }
              if (value.trim().length === 0) {
                return callback(new Error('演出日期不能为空'))
              }
              return callback()
            }
          }
        ],
        show_times: [
          {
            trigger: 'blur',
            validator(rule, value, callback) {
              if (typeof value !== 'string') {
                return callback(new Error('演出日期不能为空'))
              }
              if (value.trim().length === 0) {
                return callback(new Error('演出日期不能为空'))
              }
              return callback()
            }
          }
        ],
        sort: [
          {
            trigger: 'blur',
            validator(rule, value, callback) {
              if (value.trim().length === 0) {
                return callback()
              }
              if (!/^\d+$/.test(value)) {
                return callback(new Error('请输入数值'))
              }
              return callback()
            }
          }
        ]
      },
      loading: false
    }
  },
  methods: {
    async enter() {
      try {
        await this.$refs.form.validate()
        try {
          this.loading = true
          await addShow(this.form)
          this.loading = false
          this.$emit('enter')
        } catch (error) {
          this.$errorMessage()
          this.loading = false
        }
      } catch (error) {
        this.$errorMessage('表单填写有误')
      }
    },
    picturesUpdate(fileList) {
      this.pictures = fileList
      if (this.pictures.length > 0) {
        this.form.pictures = this.pictures[0].url
      } else {
        this.form.pictures = ''
      }
      this.$refs.form.validateField(['pictures'])
    },
    cancel() {
      this.$emit('cancel')
    }
  }
}
</script>
